package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminCompanyTypeDao;
import ex.com.jm.food.entity.CompanyType;

public class AdminCompanyTypeDaoImpl extends HibernateDaoSupport implements
		AdminCompanyTypeDao {

	public AdminCompanyTypeDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<CompanyType> getCompanyTypes(final int initPage,final int pageRow,
			final String name,final Integer status,final String order,final String sort) {
		// TODO Auto-generated method stub
		Object comtypes=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.ComTypeDTO(comtypeId,comtypeName,created,status) from CompanyType where 1=1  ";
				if(name!=null&&!name.equals("")){
					hql+=" and comtypeName like '%"+name+"%'";
				}
				if(status!=null&&!status.equals("")){
					hql+=" and status="+status;
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by "+order+" "+sort;
				}else{
					hql+=" order by created desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<CompanyType>) comtypes;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int getcomTypeCount(final String name,final Integer status) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from CompanyType where 1=1";
				if(name!=null&&!name.equals("")){
					hql+=" and comtypeName like '%"+name+"%'";
				}
				if(status!=null&&!status.equals("")){
					hql+=" and status="+status;
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveComType(CompanyType companyType) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(companyType);
	}

	@Override
	public void updateComtype(CompanyType companyType) {
		// TODO Auto-generated method stub
        this.getHibernateTemplate().update(companyType);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateStatus(final Integer status,final Integer id) {
		// TODO Auto-generated method stub
         int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("update CompanyType set status="+status+" where comtypeId="+id);
				return query.executeUpdate();
			}
		});
         if(count==1){
        	 return true;
         }
		return false;
	}

	@Override
	public CompanyType editCompanyType(Integer id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(CompanyType.class, id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public CompanyType checkName(final String name) {
		// TODO Auto-generated method stub
		CompanyType companyType=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from CompanyType where comtypeName='"+name+"'");
				return query.uniqueResult();
			}
		});
		return companyType;
	}

}
